package com.hifar.test.syncData.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hifar.common.QueryParamBean;
import com.hifar.model.VO.QueryParamsVO;
import com.hifar.test.syncData.pojo.HfEnvSyncTableConfig;
import com.hifar.test.syncData.pojo.VO.BatchUpdateSyncTimeVO;
import com.hifar.test.syncData.pojo.VO.HfEnvSyncTableConfigAddVO;
import com.hifar.test.syncData.pojo.VO.TableFieldVO;
import com.hifar.test.syncData.service.IHfEnvSyncTableConfigService;
import com.hifar.sys.BusiResponse;
import com.hifar.sys.PageResult;
import com.hifar.sys.SystemUtil;
import com.hifar.validate.CreateGroup;
import com.hifar.validate.UpdateGroup;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Locale;

/**
 * @author zhuWeiWei
 * @description 数据同步配置
 * @date 2025/4/11 16:42
 */
@Api("数据同步配置")
@RestController
@RequestMapping("/api/busi/test/syncConfig")
public class HfEnvSyncTableConfigController {
    @Autowired
    private IHfEnvSyncTableConfigService syncTableConfigService;

    @PostMapping("/listPage")
    @ApiOperation("列表查询")
    public BusiResponse listPage(@RequestBody QueryParamsVO queryParamsVO) {
        QueryParamBean<HfEnvSyncTableConfig> queryParamBean = new QueryParamBean<>(queryParamsVO, HfEnvSyncTableConfig.class);
        QueryWrapper<HfEnvSyncTableConfig> queryWrapper = queryParamBean.getQueryWrapper();
        Page<HfEnvSyncTableConfig> page = queryParamBean.getPage();
        Page<HfEnvSyncTableConfig> pageData = syncTableConfigService.page(page, queryWrapper);
        PageResult pageResult = SystemUtil.parsePage(page, pageData);
        return BusiResponse.OK(pageResult);
    }


    @PostMapping("/batchAdd")
    @ApiOperation("批量新增")
    public BusiResponse batchAdd(@RequestBody List<HfEnvSyncTableConfigAddVO> tableConfigAddVOList) {
        syncTableConfigService.batchAdd(tableConfigAddVOList);
        return BusiResponse.OK();
    }


    @PostMapping("/add")
    @ApiOperation("新增")
    public BusiResponse add(@RequestBody @Validated(CreateGroup.class) HfEnvSyncTableConfig tableConfig) {
        syncTableConfigService.save(tableConfig);
        return BusiResponse.OK();
    }

    @PostMapping("/edit")
    @ApiOperation("编辑")
    public BusiResponse edit(@RequestBody @Validated(UpdateGroup.class) HfEnvSyncTableConfig tableConfig) {
        syncTableConfigService.updateById(tableConfig);
        return BusiResponse.OK();
    }


    @ApiOperation("删除")
    @PostMapping("/delete")
    public BusiResponse delete(@RequestBody List<String> idList) {
        syncTableConfigService.removeByIds(idList);
        return BusiResponse.OK();
    }

    @ApiOperation("查询所有表")
    @PostMapping("/listPageTable")
    public BusiResponse listPageAllTable(@RequestBody QueryParamsVO queryParamsVO) {
        QueryParamBean<HfEnvSyncTableConfig> queryParamBean = new QueryParamBean<>(queryParamsVO);
        queryParamBean.addField("tableName","t");
        queryParamBean.addField("comments","c");
        Page<HfEnvSyncTableConfig> page = queryParamBean.getPage();
        QueryWrapper<HfEnvSyncTableConfig> queryWrapper = queryParamBean.getQueryWrapper();
        Page<HfEnvSyncTableConfig> pageData = syncTableConfigService.listPageAllTable(page, queryWrapper);
        PageResult pageResult = SystemUtil.parsePage(page, pageData);
        return BusiResponse.OK(pageResult);
    }


    @ApiOperation("查询表字段")
    @GetMapping("/listPageTableField")
    public BusiResponse listTableField(@RequestParam("tableName") String tableName) {
        List<TableFieldVO> listData = syncTableConfigService.listTableField(tableName.toUpperCase(Locale.ROOT));
        return BusiResponse.OK(listData);
    }

    @ApiOperation("批量更新最后导出时间")
    @PostMapping("/batchUpdateSyncTime")
    public BusiResponse batchUpdateSyncTime(@RequestBody @Validated BatchUpdateSyncTimeVO batchUpdateVO) {
        List<String> idList = batchUpdateVO.getIdList();
        if (idList == null || idList.isEmpty()) {
            return BusiResponse.error("请选择需要修改的配置");
        }
        
        if (idList.size() > 100) {
            return BusiResponse.error("单次最多选择100条记录");
        }
        
        syncTableConfigService.batchUpdateSyncTime(idList, batchUpdateVO.getLastSyncTime());
        return BusiResponse.ok("批量修改成功，共修改" + idList.size() + "条记录");
    }
}
